// @algorithm @lc id=337 lang=cpp 
// @title house-robber-iii


// @test([3,2,3,null,3,null,1])=7
// @test([3,4,5,1,3,null,1])=9
class Solution {
public:
    pair<int,int>/*sel, nosel*/ LRD(TreeNode * node){
        if(!node) return {0, 0};
        auto L = LRD(node->left);
        auto R = LRD(node->right);
        auto sel = node->val + L.second + R.second;
        auto nosel = max(L.first, L.second) + max(R.first, R.second);
        return {sel, nosel};
    }
    int rob(TreeNode* root) {
        auto r = LRD(root);
        return max(r.first, r.second);
    }
};
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */